package icasue.net.socket.driver;

import icasue.net.base.annotations.NotNull;
import icasue.net.protocol.carfun.commands.Command;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/3/19 下午3:35
 * @UpdateDate:
 * @Description:
 */
public interface CommandDriver<C extends Command> {

    /**
     * get server local's net address.
     * @return
     */
    default String getNetAddress(){
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Get commands which supported by current driver.
     * @return
     */
    C[] getSupportCommands();

    /**
     * Default method to check if current CommandDriver is support for Command's deal processor.
     * @param command
     * @return
     */
    default C commandSupport(String command){
        C[] supportCommands = getSupportCommands();
        if(supportCommands != null){
            for (C supportCommand : supportCommands) {
                if(supportCommand.getCommand().equals(command))
                    return supportCommand;
            }
        }
        return null;
    }

    /**
     * Default method to choose a driver which supported current command.
     * @param command
     * @return
     */
    default CommandDriver driverSupport(@NotNull C command){
        C[] supportCommands = getSupportCommands();
        if(supportCommands != null){
            for (C supportCommand : supportCommands) {
                if(supportCommand.equals(command))
                    return this;
            }
        }
        return null;
    }
}

